package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Untitled {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled() throws Exception {
	goToMainPage();
    logInAs("admin", "admin");
    logOut();
  }

  @Test
public void TestPasswordChange() {
		goToMainPage();
	    logInAs("admin", "admin");
	    goToUserProfile();
	    changeEmailTo("admin@adnin.ru");
	    
	    logOut();
  }

private void changeEmailTo(String string) {
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys("admin@admin.ru");
    driver.findElement(By.name("submit")).click();
}

private void goToUserProfile() {
    driver.findElement(By.linkText("My profile")).click();
}

private void logOut() {
	driver.findElement(By.linkText("Log out")).click();
    driver.switchTo().alert().accept();
}

private void logInAs(String username, String password) {
	driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys(username);
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys(password);
    driver.findElement(By.name("submit")).click();
}

private void goToMainPage() {
    driver.get(baseUrl + "/php4dvd/");
}

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
